/*----------------------------------------------------------------------------
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in
compliance with the License. You may obtain a copy of the 
License at http://www.mozilla.org/MPL/
	
Software distributed under the License is distributed on 
an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, 
either express or implied. 
See the License for the specific language governing rights and 
limitations under the License.
	
The Original Code is Fever RS ( ActionScript 3 ) Framework code.
	
The Initial Developer of the Original Code is Romain Ecarnot.
Portions created by Initial Developer are Copyright (C) 2006
the Initial Developer. All Rights Reserved.
	
Contributor(s):
	
Unless required by applicable law or agreed to in writing, 
software distributed under the License is distributed on 
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 
OF ANY KIND, either express or implied.
See the License for the specific language governing permissions 
and limitations under the License.
----------------------------------------------------------------------------*/

package fever.data.hasher 
{
	import com.bourre.error.UnimplementedVirtualMethodException;
	
	import fever.utils.reflect.ClassInfo;	

	/**
	 * Converts custom instance to be <strong>HashAlgorithm</strong> 
	 * compliant.
	 * 
	 * <p>HashAlgorithmAdapter instance can be used by 
	 * <strong>MessageDigest</strong> system.</p>
	 * 
	 * @see fever.data.hasher.MessageDigest
	 * @see fever.data.hasher.HashAlgorithm
	 * 
	 * @author Romain Ecarnot
	 */
	public class HashAlgorithmAdapter implements HashAlgorithm
	{
		//--------------------------------------------------------------------
		// Constants
		//--------------------------------------------------------------------
		
		/**
		 * Method name for the "hash()" method of 
		 * <strong>HashAlgorithm</strong> <code>hash</code> method.
		 */
		public static const HASH_METHODE_NAME : String = "hash";
		
		
		//--------------------------------------------------------------------
		// Private properties
		//--------------------------------------------------------------------
		
		private var _algorithm : Object;
		
			
		//--------------------------------------------------------------------
		// Public API
		//--------------------------------------------------------------------
		
		/**
		 * Constructs new HashAlgorithm Adapter
		 * 
		 * @param hasher Custom hasher object
		 * 
		 * @throws Lowra UnimplementedVirtualMethodException 
		 * if <code>hasher</code> has no <code>hash()</code> implemented method.<br />
		 * Methods signatures are also check, and must be equal than the 
		 * <strong>HashAlgorithm</strong> methods signatures.</p>
		 */
		public function HashAlgorithmAdapter( hasher : Object )
		{
			var o : Object = null;
			
			if( hasher != null )
			{
				var info : ClassInfo = ClassInfo.describe( hasher );
				
				if( info.containsMethod( HASH_METHODE_NAME ) )
				{
					if( info.getMethod( HASH_METHODE_NAME ).parameters.length == 2 )
					{
						o = hasher;
					}
				}
			}
			
			if( o != null ) _algorithm = o;
			else throw new UnimplementedVirtualMethodException(	"Instance does not implements 'HashAlgorithm()' interface" );
		}
		
		/**
		 * @inheritDoc
		 */
		public function hash( source : String ) : String
		{
			return _algorithm.hash( source );
		}
	}
}
